<?php
include("include/conn.php");
  require('include/Smarty/libs/Smarty.class.php');

  $msg = '';
  //print_r($_REQUEST);
  if(isset($_POST['submit']) && $_POST['party'] == 0)
  {
   //$msg = "<center><div class='clean-gray'>Select Party Name ...!!!</div></center><br/>";
  }
  else if(isset($_POST['submit']) && isset($_POST['billDateYear']) && isset($_POST['billDateMonth']) && isset($_POST['billDateDay']) && $_POST['party'] != "")
  {
  	$billDateYear  = $_POST['billDateYear'];
  	$billDateMonth = $_POST['billDateMonth'];
  	$billDateDay   = $_POST['billDateDay'];
	  $updateQueryBillBook = "UPDATE billbook 
                                   SET partyId  = '".$_REQUEST['party']."',
                                       billbookdate = '".$billDateYear."-".$billDateMonth."-".$billDateDay."'
                                 WHERE billbookid = '".$_REQUEST['billBookIdEdit']."'";
	  $updateQueryBillBookRequest = mysql_query($updateQueryBillBook);
	  if(!$updateQueryBillBookRequest)
	  {
	    die("Update Query Not Inserted : ".mysql_error());
	  }
	  else
	  {
	 		header("Location:billBookList.php");
	  }
	}
  $partyIdCount = "";
  $partyId      = "";
  $partyName    = "";
  $i = 0;
  $selectQuerypartyName = "SELECT partyId,partyName 
                             FROM party
                            ORDER BY partyName";
  $selectQuerypartyNameResult = mysql_query($selectQuerypartyName);
  $partyNameRow = mysql_num_rows($selectQuerypartyNameResult);
  if($partyNameRow > 0)
  {
    while($partyNameRow = mysql_fetch_array($selectQuerypartyNameResult))
    {
      $partyId[$i]    = $partyNameRow['partyId'];
      $partyName[$i]  = $partyNameRow['partyName'];
      $i++;
    }
  }

	$billBookId      = '';
	$partyIdSelected      = '';
	$billDate      = '';
	$selectQueryBillBook = "SELECT billbookid,partyId,billbookdate 
	                          FROM billbook
	                         WHERE billbookid = ".$_REQUEST['billBookId'];
	$selectQueryBillBookResult = mysql_query($selectQueryBillBook);
	$selectQueryBillBookNumRow = mysql_num_rows($selectQueryBillBookResult);
  $partyIdSelected = 0;
	if($selectQueryBillBookNumRow > 0)
	{
		while($billBookRow = mysql_fetch_array($selectQueryBillBookResult))
		{
			$billBookId      = $billBookRow['billbookid'];
      $partyIdSelected = $billBookRow['partyId'];
      $billDate    = substr($billBookRow['billbookdate'],0,10);
		}
	}
		
  $smarty = new Smarty;
  require('bottom.php');
  $smarty->assign("l_editBillBook",$l_editBillBook);
  $smarty->assign("l_partyName",$l_partyName);
  $smarty->assign("l_partySelect",$l_partySelect);
  $smarty->assign("l_billBookDate",$l_billBookDate);

  $smarty->assign("msg",$msg);
  $smarty->assign("billBookId",$billBookId);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("partyName",$partyName);
  $smarty->assign("partyIdSelected",$partyIdSelected);
  $smarty->assign("billDate",$billDate);

	$smarty->display('billBookEdit.tpl');
?>
